package com.bd.scm.module.oauth2.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bd.scm.domain.dto.numberOfRuns.ZtBanCiDto;
import com.bd.scm.module.common.BaseController;
import com.bd.scm.module.login.service.LoginService;
import com.bd.scm.module.oauth2.HttpUtil;
import com.bd.scm.module.utils.EncrypDES;
import com.bd.scm.module.utils.ResultUtil;
import com.bd.utils.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLEncoder;

/**
 * Created with IntelliJ IDEA.
 * User: _Cps
 * Date: 2020.08.28 11:49
 */
@RestController
@RequestMapping("/oauth2")
public class Oauth2Controller extends BaseController {

    /**
     * 登录Service
     */
    @Autowired
    private LoginService loginService;

    @RequestMapping("/home")
    public void home(HttpServletRequest request,HttpServletResponse response) throws IOException {
//        String server = "http://localhost:8000/";
        // 如果没有本地用户会话，则向派拉请求登录信息
        if (request.getSession().getAttribute("user") == null) {
            String url = "http://116.246.9.157:8070/profile/oauth2/authorize?client_id=100015&redirect_uri="
                    + URLEncoder.encode("http://hn2n11kgkj.52http.net/oauth2/callback", "utf-8")
                    + "&response_type=code&target_uri="
                    + URLEncoder.encode("http://192.168.2.100:8000/oauthLogin", "utf-8");
            response.sendRedirect(url);
            return;
        }
    }

    @RequestMapping("/callback")
    public void callback(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException {
        response.setContentType("text/plain;charset=UTF-8");
        // 保存最终要跳转的地址
        String target = request.getParameter("target_uri");
        PrintWriter out = response.getWriter();
        String url = "http://116.246.9.157:8070/profile/oauth2/accessToken?client_id=100015&code="
                + request.getParameter("code")
                + "&client_secret=cb3ccd9a-b0a2-42a8-bc86-4dc8e0ed4278&redirect_uri="
                + URLEncoder.encode(target, "utf-8");
        String res = HttpUtil.httpPost(url, "");
        String token = res.substring(res.indexOf('=') + 1, res.indexOf('&'));
//        System.out.println("<<<================== "+token+" ==================>>>");
        url = "http://116.246.9.157:8070/profile/oauth2/profile?access_token=" + token;
        res = HttpUtil.httpPost(url, "");
        JSONObject jsonObject = JSONObject.parseObject(res);
        if(jsonObject.containsKey("error")){
            //跳转验证错误页面
            System.out.println("<<<================== 1 ==================>>>");
        }else{
            //{"accountId":"02356","uid":"mingz05@hascovision.com","mobile":"15921164689","userName":"张铭","email":"mingz05@hascovision.com"}
            System.out.println("<<<================== "+jsonObject.getString("accountId")+" ==================>>>");
            String accountId = jsonObject.getString("accountId");
            try {
                EncrypDES des1 = new EncrypDES();// 使用默认密钥
                accountId = des1.encrypt(accountId);
                response.sendRedirect(target+"?username="+accountId);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }
}
